export namespace Attribute {
    export enum EnumAttributeType {
        // 钱
        money = 100,
        // 跑步加速
        extraMoveSpeed = 101,
        // 跳强度
        extraJumpForce = 102,
    }

    // 存储属性
    const PlayerStashAttribute: Set<EnumAttributeType> = new Set([
        Attribute.EnumAttributeType.money,
    ])

    // 此属性是否需要存储
    export function IsStashAttribute(type: Attribute.EnumAttributeType): boolean {
        return PlayerStashAttribute.has(type)
    }

    // type:属性类型-value:属性值
    export type AttributeArray = { [type: number]: number }

    // 属性集合
    export class AttributeValueObject {
        attributeArray: Attribute.AttributeArray

        constructor() {
            this.attributeArray = {}
        }

        setAttribute(type: Attribute.EnumAttributeType, value: number) {
            this.attributeArray[type] = value
        }

        getValue(type: Attribute.EnumAttributeType): number {
            return this.attributeArray[type] ? this.attributeArray[type] : 0
        }

        addValue(type: Attribute.EnumAttributeType, value: number) {
            if(value == 0) return
            
            let val = this.getValue(type)
            this.setAttribute(type, value + val)
        }

        reduceValue(type: Attribute.EnumAttributeType, value: number) {
            let val = this.getValue(type)
            this.setAttribute(type, Math.max(0, val - value))
        }

        // 合并other属性
        bringIn(other:AttributeValueObject){
            for (let type in other.attributeArray) {
                let val = other.attributeArray[type];
                this.addValue(Number(type), val);
            }
        }

        // 清空(值)复用
        clear():AttributeValueObject{
            for (const key in this.attributeArray) {
                this.attributeArray[key] = 0
            }
            return this
        }
    }
}
